<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.9
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

class JFX_Module_Blockstyle_Widget_Image extends JFX_Module_Blockstyle_Widget_Base
{
	public $id = 'image';
	public $value = '';
	public $allowUpload = true;
	public $filepicker = null;
	public $filename = '';
	public $path = '';
	public $url = '';
	public $previewImageNodeId = 'JFX_Module_Blockstyle_Previewimage';

	public function __construct(JFX_Module_Blockstyle_Property_Base $property){
		parent::__construct($property);
		$this->privateProperties[] = 'filepicker';
		$this->runtimeProperties[] = 'filepicker';
	}

	public function updateFromJson($widget){
		parent::updateFromJson($widget);
		$IMAGER = JFX::registry('JFX_Module_Imager');
		$file = JFX_Data::getFile($this->path, $this->filename, $IMAGER->keyname);
		if(is_object($file) && $file instanceof JFX_Data_File){
			$this->url = $IMAGER->imageUrl.'/serve.php?absolutepath='.urlencode($file->getAbsolutePath());
		}else{
			$BLOCKSTYLE = JFX::registry('JFX_Module_Blockstyle');
			$this->url = $BLOCKSTYLE->imageUrl.'/canvas-crumpled.png';
		}
		
	}


	public function buildImageList(){

		
		$BLOCKSTYLE = JFX::registry('JFX_Module_Blockstyle');
		$IMAGER = JFX::registry('JFX_Module_Imager');
		$tree = $IMAGER->getFolderHeirarchy();

		$folderUrl = 'javascript: void(null);';
		$fileUrl = 'javascript: void(null);';

		$filePath = ':|url|:';

		$folderClick = 'AntzBSWidgetImage.selectFolder(\':|folder|:\', \''.$this->instanceId.'\');';
		$fileClick = 'AntzBSWidgetImage.selectFile(\''.$filePath.'\', \''.$this->instanceId.'\', \':|path|:\', \':|filename|:\');';


		$this->filepicker = $IMAGER->makeTree($tree, $IMAGER, true, $folderUrl, $fileUrl, $folderClick, $fileClick);

	}
}